/**
  net_utils.java is part of Marioneta.

  Marioneta is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License.

  Marioneta is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
  
  @author martin
   Martin Edmundo Barriga Orozco, 2008
   martin.barriga@gulmore.org
  
  
  netUtils: Esta clase es la base de las comunicaciones, entre sus obscuros 
  secretos nos da la posibilidad de hacer envio/recepcion de cadenas y bytes.
 */

package escritorioremoto;
import java.net.Socket;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;

public abstract class netUtils{
    private Socket listenSocket;
    
    /**
     * identificador numerico de este servidorcito, en un contexto con un global
     * server, sirve para que este ultimo asigne un id a cada cliente que el atiende
     * en otro contexto, se puede omitirse su uso.
     */
    private int id_connect;
    
    /*
     * Este metodo abstracto debera definir el protocolo, de saludo o hadshake
     * que se debera llevar a cabo para entenderse con un cliente.
     */
    public abstract void initProc();
    
    //Inicializador de valores
    public void initVal( Socket sk, int id ){
        listenSocket = sk;
        id_connect = id;
    }
    
    
    public void sendByte(byte[] dato) throws IOException {
        if(dato.length > 0 ){
            OutputStream ps = listenSocket.getOutputStream();
            DataOutputStream flujo= new DataOutputStream( ps );
            flujo.write(dato, 0, dato.length );
        }
    }
    
    public byte[] recibeByte() throws IOException {
        byte[] captura = new byte[4096];
        DataInputStream datos = new DataInputStream( listenSocket.getInputStream() );
        datos.read(captura);
        System.out.print("Tamaño de lo que lee: "+ captura.length);
        return captura;  
    }
    
    public InputStream getInputStreamSocket() throws IOException {
        System.out.print("Valor del imputstream: " + listenSocket.toString()  );
        return listenSocket.getInputStream();
    }
    
    public void sendStr(String strSend) throws IOException {
        if( !strSend.equals("") ){
            OutputStream ps = listenSocket.getOutputStream();
            DataOutputStream flujo = new DataOutputStream( ps );
            flujo.writeUTF( strSend );
        }
        else{
            OutputStream ps = listenSocket.getOutputStream();
            DataOutputStream flujo = new DataOutputStream( ps );
            flujo.writeUTF( "algo paso" );
        }
    }
    
    public String recibeStr() throws IOException {
        String buffer="";
        DataInputStream datos = new DataInputStream( listenSocket.getInputStream() );
        buffer = datos.readUTF();
        return buffer;
    }
    
    public int getID(){
        return id_connect;
    }
    
}
